package com.lz.football.module.youngTrain.view
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.Reflection;
	import com.common.utils.timeUtils.TimerManager;
	import com.global.GlobalDef;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.YoungTrainEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.model.youngTrain.vo.YoungTrainInfoVo;
	import com.lz.football.module.youngTrain.IYoungTrainView;
	
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.core.TitleWindow;
	import lz.controls.managers.PopUpManager;

	/**
	 *青训营 
	 * @author lake
	 * 
	 */	
	public class YoungTrainView extends TitleWindow implements IYoungTrainView
	{
		/**
		 *背景 
		 */		
		private var bgMc 				:MovieClip;
		/**
		 * 离开按钮
		 */		
		private var leaveBtn			:SimpleButton;
		/**
		 *升级 
		 */		
		private var gradeBtn			:SimpleButton;
		/**
		 *增加球童 
		 */		
		private var addPlayerChBtn		:SimpleButton;
		/**
		 *全部签约 
		 */		
		private var allGetBtn			:SimpleButton;
		/**
		 *冷却时间mc 
		 */		
		private var dropTimeMc			:MovieClip;
		/**
		 *冷却时间 
		 */		
		private var txtLeftTime		:TextField;
		/**
		 *冷却时间条 
		 */		
		private var txtTimeDesc 		:TextField;
		/**
		 *升级条件 
		 */		
		private var txtFactor			:TextField;
		/**
		 *下级描述 
		 */		
		private var txtNextDesc		:TextField;
		/**
		 *生产球童数描述 
		 */		
		private var txtChildNum		:TextField;
		/**
		 *场上可同时训练球童数 
		 */		
		private var txtTrainNum		:TextField;
		/**
		 *增加每天生产次数 
		 */		
		private var addNumBtn			:SimpleButton;
		/**
		 *剩余时间 秒 
		 */		
		private var leftTime			:int;
		/**
		 *显示球童面板 
		 */		
		private var showPlayerChildPanel 	:ShowPlayerChildPanel;
		/**
		 * 出售提示面板
		 */		
		private var saleResultPanel		:SaleResultPanel
		
		public function YoungTrainView()
		{
			super();
		}
		/**
		 * 初始化
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.module.youngTrain.YoungTrainSkin");
			this.addChild(_symbolSkin);
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			leaveBtn 		= _symbolSkin.leaveBtn;
			gradeBtn		= _symbolSkin.gradeBtn;
			dropTimeMc	    = _symbolSkin.dropTimeMc;
			txtLeftTime		= dropTimeMc.txtLeftTime;
			txtChildNum		= _symbolSkin.txtChildNum;
			addPlayerChBtn	= _symbolSkin.addPlayerChBtn;
			allGetBtn		= _symbolSkin.allGetBtn;
			addNumBtn		= _symbolSkin.addNumBtn;
			txtTrainNum		= _symbolSkin.txtTrainNum;
			dropTimeMc.visible = false;
			txtFactor 		= _symbolSkin.gradeMc.txtFactor;
			txtNextDesc		= _symbolSkin.nextMc.txtNextDesc;
			//设置可拖动
			dragEnabled = true;
			//设置关闭按钮
			setCloseBtn(_symbolSkin.closeBtn);
			//设置坐标不自适应
			resizeEnabled = false;
			//添加事件
			addEvtListener();
			//浅入
			fadeIn();
			//球童信息
			showPlayerChildPanel = new ShowPlayerChildPanel();
			showPlayerChildPanel.initUI();
			showPlayerChildPanel.showPlayer();
			_symbolSkin.addChild(showPlayerChildPanel);
			showPlayerChildPanel.x = 14;
			showPlayerChildPanel.y = 155;
			//TIPs
			var tipsInfo :String = "<p align='left'>购买一个球童位且将球童出现等级提升为红色！\n可以产生最高品质为红色的球星！</p>";
			DisplayMouseEventManager.instance.addTipsDisplay(addPlayerChBtn,tipsInfo);
			tipsInfo = "全部签约成熟的球童";
			DisplayMouseEventManager.instance.addTipsDisplay(allGetBtn,tipsInfo);
			tipsInfo = "购买一次球童数";
			DisplayMouseEventManager.instance.addTipsDisplay(addNumBtn,tipsInfo);
			//显示信息
			showBaseInfo();
		}
		/**
		 *显示基本信息
		 * 
		 */		
		private function showBaseInfo():void
		{
			var baseInfo :YoungTrainInfoVo = AppData.getInstance().youngTrainManager.youngTrainInfoVo;
			_symbolSkin.lvMc.gotoAndStop(baseInfo.level);
			var info :String = "" + baseInfo.coin;
			info += "欧元，" + baseInfo.fame + "声望";
			txtFactor.htmlText = info;
			info = "有几率出现" + AppData.getInstance().youngTrainManager.nextCColors[baseInfo.level] + "球童";
			if(AppData.getInstance().youngTrainManager.nextAddCNums[baseInfo.level] > 0)
			{
				info += "，场上存在球童的上限+1";
			}
			txtNextDesc.htmlText = info;
			info = "当天可产出球童数量：" + baseInfo.caddie + "/" + baseInfo.caddiecount;
			txtChildNum.htmlText = info;
			info = "场上可存在球童数量：" + (baseInfo.caddienum + baseInfo.numcashpoint);
			txtTrainNum.htmlText = info;
			//当前时间
			var curSeconds 		:Number = AppData.getInstance().time;
			//当前时间与上次升级时间间隔 秒
			var grapTime :Number = curSeconds - baseInfo.pretime;
			//如果大于冷却时间 可以再次升级
			if(grapTime >= baseInfo.freezingTime)
			{
				dropTimeMc.visible = false;
				gradeBtn.visible = true;
				TimerManager.instance.removeFunction(onTiming);
				txtLeftTime.removeEventListener(TextEvent.LINK,onDropTimeHandler);
			}
			else//显示冷却时间
			{
				dropTimeMc.visible = true;
				leftTime = baseInfo.freezingTime - grapTime;
				gradeBtn.visible = false;
				txtLeftTime.htmlText = "" + DateUtil.formatShowHMS(leftTime,true);
				txtLeftTime.addEventListener(TextEvent.LINK,onDropTimeHandler);
				TimerManager.instance.addFunction(onTiming);
			}
			if(baseInfo.level >= 9)
			{
				gradeBtn.visible = false;
				//_symbolSkin.nextMc.visible = false;
				//_symbolSkin.gradeMc.visible = false;
				TimerManager.instance.removeFunction(onTiming);
				dropTimeMc.visible = false;
			}
			var tipsInfo :String;
			if(baseInfo.numcashpoint == 5)
			{
				tipsInfo = "<p align='left'>购买一个球童位且将球童出现等级提升为黑色！\n可以产生最高品质为黑色的球星！</p>";
				DisplayMouseEventManager.instance.addTipsDisplay(addPlayerChBtn,tipsInfo);
			}
			else if(baseInfo.numcashpoint > 5)
			{
				tipsInfo = "<p align='left'><font color='#ff0000'>球童数已满。</font>\n当前可以产生最高品质为黑色的球星！</p>";
				DisplayMouseEventManager.instance.addTipsDisplay(addPlayerChBtn,tipsInfo);
			}
		}
		/**
		 *时间计时 
		 */		
		private function onTiming():void
		{
			leftTime --;
			if(leftTime <= 0)
			{
				txtLeftTime.visible = false;
				txtTimeDesc.visible = false;
				gradeBtn.visible = true;
				TimerManager.instance.removeFunction(onTiming);
			}
			else
			{
				txtLeftTime.htmlText = "<u><a href='event:dropTime'>" + DateUtil.formatShowHMS(leftTime,true) + "</a><u>";
			}
		}
		/**
		 *去除冷却时间 
		 * @param e
		 * 
		 */		
		private function onDropTimeHandler(e :Event):void
		{
			var info :String = "您确定花费200点券去除冷却时间?";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,dropTimeAlertHandler);
		}
		/**
		 * 去除冷却时间 确认
		 * @param type
		 * 
		 */		
		private function dropTimeAlertHandler(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new ModuleEvent(ModuleEvent.DROP_TIME,true,false,BuildingType.YOUNGTRAIN));
			}
		}
		/**
		 * 
		 * 添加事件
		 */		
		public function addEvtListener():void
		{
			gradeBtn.addEventListener(MouseEvent.CLICK,gradeClickHandler);
			addPlayerChBtn.addEventListener(MouseEvent.CLICK,addPlayerHandler);
			allGetBtn.addEventListener(MouseEvent.CLICK,allGetHandler);
			addNumBtn.addEventListener(MouseEvent.CLICK,addNumHandler);
			AppData.getInstance().youngTrainManager.addEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *点击升级 
		 * @param e
		 * 
		 */		
		private function gradeClickHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new YoungTrainEvent(YoungTrainEvent.GRADE_UP));
			gradeBtn.mouseEnabled = false;
		}
		/**
		 *增加球童 
		 * @param e
		 * 
		 */		
		private function addPlayerHandler(e :MouseEvent):void
		{
			var cashNum :int = AppData.getInstance().youngTrainManager.youngTrainInfoVo.numcashpoint;
			if(cashNum>=6)
			{
				Alert.showErrorTip("可增加球童的次数已满");
				return ;
			}
			var colorName:String = "红色";
			if(cashNum == 5)
			{
				colorName = "黑色";
			}
			var info :String = "<p align='left'>购买一个球童位且将球童出现等级提升为"+ colorName +"！可以产生最高品质为"+ colorName +"的球星！</p>";
			info += "\n<p align='center'><font color='#ff0000'>是否花费" + AppData.getInstance().youngTrainManager.openNumPrice + "点券?</font></p>";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,addPlayerAlertHandler);
		}
		/**
		 * 增加球童  确认
		 * @param type
		 * 
		 */		
		private function addPlayerAlertHandler(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new YoungTrainEvent(YoungTrainEvent.ADD_PLAYER));
			}
		}
		/**
		 *全部签约 
		 * @param e
		 */		
		private function allGetHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new YoungTrainEvent(YoungTrainEvent.GET_PLAYER_ALL));
		}
		/**
		 *增加次数 
		 * @param e
		 * 
		 */		
		private function addNumHandler(e :MouseEvent):void
		{
			var info :String = "您确定花费"+AppData.getInstance().youngTrainManager.addNumPrice+"W欧元增加一次每天产出球童数量?";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,addNumAlertHandler);
		}
		/**
		 * 增加次数 确认
		 * @param type
		 * 
		 */		
		private function addNumAlertHandler(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new YoungTrainEvent(YoungTrainEvent.ADD_PLAYER_GROW_NUM));
			}
		}
		/**
		 *数据改变 
		 * @param e
		 * 
		 */		
		private function dataChangeHandler(e :Event):void
		{
			showBaseInfo();
			showPlayerChildPanel.showPlayer();
		}
		/**
		 * 
		 * 删除事件
		 */		
		public function removeEvtListener():void
		{
			gradeBtn.removeEventListener(MouseEvent.CLICK,gradeClickHandler);
			addPlayerChBtn.removeEventListener(MouseEvent.CLICK,addPlayerHandler);
			allGetBtn.removeEventListener(MouseEvent.CLICK,allGetHandler);
			addNumBtn.removeEventListener(MouseEvent.CLICK,addNumHandler);
			AppData.getInstance().youngTrainManager.removeEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 * 关闭按钮响应
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE,true));
			super.onCloseHandler(e);
		}
		/**
		 * 销毁
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			showPlayerChildPanel.destroyView();
			showPlayerChildPanel = null;
			DisplayMouseEventManager.instance.removeTipsDisplay(addPlayerChBtn);
			DisplayMouseEventManager.instance.removeTipsDisplay(allGetBtn);
			DisplayMouseEventManager.instance.removeTipsDisplay(addNumBtn);
			if(saleResultPanel != null)
			{
				saleResultPanel.destroyView();
				saleResultPanel = null;
			}
			TimerManager.instance.removeFunction(onTiming);
		}
		/**
		 * 显示某一类型
		 * @param $obj
		 */		
		public function showType($obj:Object):void
		{
		}
		//////////////////////implements///////////////////////////
		/**
		 *升级 
		 */		
		public function gradeUp():void
		{
			gradeBtn.mouseEnabled = true;
			showBaseInfo();
			showPlayerChildPanel.showPlayer();
		}
		/**
		 *去除时间 
		 * 
		 */		
		public function dropTime():void
		{
			showBaseInfo();
		}
		/**
		 *出售球员
		 * @param resObj
		 * 
		 */		
		public function salePlayer(resObj :Object):void
		{
			if(resObj.retcode == 0)//成功
			{
				showPlayerChildPanel.updatePlayer();
				if(saleResultPanel == null)
				{
					saleResultPanel = new SaleResultPanel();
					saleResultPanel.initUI();
				}
				saleResultPanel.setDescInfo();
				PopUpManager.addPopUp(saleResultPanel ,GlobalDef.GameWindowContainer,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageWidth,true);
			}
		}
		/**
		 * 签约球员 
		 * @param resObj
		 * 
		 */		
		public function getPlayer(resObj :Object):void
		{
			if(resObj.retcode == 0)//成功
			{
				showPlayerChildPanel.updatePlayer();
				var stars :Array = resObj.stars;
				var getPlayerPanel :GetPlayerResultPanel;
				var i :int = 0;
				for each(var starId :int in stars)
				{
					getPlayerPanel = new GetPlayerResultPanel();
					getPlayerPanel.initUI();
					getPlayerPanel.data = {starId:starId,color:AppData.getInstance().youngTrainManager.saleColors[i]};
					PopUpManager.addPopUp(getPlayerPanel ,GlobalDef.GameWindowContainer,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageWidth,false);
					i ++;
				}
			}
		}
	}
}